package com.itheima.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * 创建索引库及映射
 * @作者 itcast
 * @创建日期 2020/11/7 8:54
 **/
public class EsDemo01 {
    RestHighLevelClient client;



    @Test
    public void createIndex() throws IOException {
//        1. 创建对应操作的请求对象   (CreateIndexRequest)
        CreateIndexRequest request = new CreateIndexRequest("user");// 传入索引库名称
//        设置setting信息
        // key value

        request.settings("{\n" +
                "    \"number_of_shards\": 1,\n" +
                "    \"number_of_replicas\": 1\n" +
                "  }", XContentType.JSON);
//                设置mapping信息
        request.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"id\":{\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"name\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"age\":{\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"gender\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"note\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }\n" +
                "  }",XContentType.JSON);
//        2. 调用客户端 执行对应请求 .indices().create();
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
//        3. 解析响应结果
        System.out.println(createIndexResponse.isAcknowledged());
    }


    @Test
    public void deleteIndex() throws IOException {
        //1. 创建一个删除索引库的请求
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        //2. 执行删除索引库请求
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

        //3. 得到响应结果
        System.out.println(delete.isAcknowledged());
    }

    /**
     * 初始化es的客户端
     */
    @Before
    public void init(){
         client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );
    }

    /**
     * 关闭客户端
     */
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
